<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>可视域分析</title>
  <link href="src/Cesium/Widgets/widgets.css" rel="stylesheet" />
  <script src="src/Cesium/Cesium.js"></script>
  <script src="src/cesium-viewshed.js"></script>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    html,
    body,
    #viewer-container {
      width: 100%;
      height: 100%;
      overflow: hidden;
    }

    #btn {
      position: absolute;
      top: 50px;
      left: 50px;
    }
  </style>
</head>

<body>
  <div id="viewer-container"></div>
  <div id="btn">
    <button onclick="setvisible('add')">添加可视域</button>
    <button onclick="setvisible('remove')">删除可视域</button>
  </div>
  <script>
    var viewer = null;
    var arrViewField = [];
    var viewModel = { verticalAngle: 90, horizontalAngle: 120, distance: 10 };
    // 开关
    function setvisible(value) {
      switch (value) {
        case 'add':
          addViewField();
          break;
        case 'remove':
          clearAllViewField();
          break;
      }
    }
    // 添加可视域
    function addViewField() {
      var e = new Cesium.ViewShed3D(viewer, {
        horizontalAngle: Number(viewModel.horizontalAngle),
        verticalAngle: Number(viewModel.verticalAngle),
        distance: Number(viewModel.distance),
        calback: function () {
          viewModel.distance = e.distance
        }
      });
      arrViewField.push(e)
    }
    // 清除可视域
    function clearAllViewField() {
      for (var e = 0, i = arrViewField.length; e < i; e++) {
        arrViewField[e].destroy()
      }
      arrViewField = []
    }
    // init
    function initPage() {
      // 切换自己的token
      Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJlYTQ2ZjdjNS1jM2E0LTQ1M2EtOWM0My1mODMzNzY3YjYzY2YiLCJpZCI6MjkzMjcsInNjb3BlcyI6WyJhc3IiLCJnYyJdLCJpYXQiOjE1OTE5NDIzNjB9.RzKlVTVDTQ9r7cqCo-PDydgUh8Frgw0Erul_BVxiS9c';
      // 初始化
      viewer = new Cesium.Viewer("viewer-container", {
        infoBox: false,
        shouldAnimate: true,
        vrButton: true,
        geocoder: false,
        homeButton: false,
        sceneModePicker: false,
        baseLayerPicker: true,
        navigationHelpButton: false,
        animation: false,
        timeline: false,
        fullscreenButton: false,
      });
      // 加载倾斜摄影 大雁塔
      var tilesets = viewer.scene.primitives.add(new Cesium.Cesium3DTileset({
        url: 'http://earthsdk.com/v/last/Apps/assets/dayanta/tileset.json'
      }));
      viewer.flyTo(tilesets)
    }
    // 
    window.onload = function () {
      initPage()
    }
  </script>
</body>

</html>